package ru.papergames.web.blines.battle;

import ru.common.hibernate.HibernateActionExecutor;
import ru.common.network.clientcore.ClientSelectorConfig;
import ru.common.network.clientcore.ClientSelectorThread;
import ru.papergames.common.form.BattleServerCredentialsForm;
import ru.papergames.common.form.NpcPlayerForm;
import ru.papergames.common.form.PcPlayerForm;
import ru.papergames.web.blines.battle.BattleServerConnectionsHolderThread.BattleServerConnectionHolder;
import ru.papergames.web.blines.battle.model.BattleOrder2;
import ru.papergames.web.blines.battle.network.ClientWebServer;
import ru.papergames.web.blines.battle.network.ClientWebServerGamePacketHandler;
import ru.papergames.web.services.GetBattleServers;

import java.util.List;

/**
 * Date: 03.01.12 17:37
 *
 * @author Alexander Troshanin
 */
public class BattleServersManager {
    private static BattleServersManager instance = null;

    private HibernateActionExecutor hiber;

    private ClientSelectorThread<ClientWebServer> selectorThread;
    private BattleServerConnectionsHolderThread connectionsHolderThread;
    private BaseFightersPoolManagerThread fightersPoolManagerThread;

    public static BattleServersManager createInstance(HibernateActionExecutor hiber) throws BattleServerManagerInitException {
        if (instance == null) {
            synchronized (BattleServersManager.class) {
                if (instance == null) {
                    try {
                        instance = new BattleServersManager(hiber);
                    } catch (Exception e) {
                        e.printStackTrace();
                        throw new BattleServerManagerInitException(e);
                    }
                }
            }
        }
        return instance;
    }

    public static BattleServersManager getInstance() {
        if (instance == null) {
            throw new RuntimeException("BattleServersManager is not created.");
        }
        return instance;
    }

    private BattleServersManager(HibernateActionExecutor hiber) throws Exception {
        this.hiber = hiber;
        ClientSelectorConfig sc = new ClientSelectorConfig();
        sc.SLEEP_TIME = 100;
        ClientWebServerGamePacketHandler handler = new ClientWebServerGamePacketHandler();
        selectorThread = new ClientSelectorThread<ClientWebServer>(sc, handler, handler, handler, null);



        connectionsHolderThread = new BattleServerConnectionsHolderThread(selectorThread, hiber.execute(new GetBattleServers()));
        fightersPoolManagerThread = new BaseFightersPoolManagerThread();
    }

    public void start() {
        selectorThread.start();
        connectionsHolderThread.start();
        fightersPoolManagerThread.start();
    }

    public void shutdown() {
        selectorThread.shutdown();
        connectionsHolderThread.shutdown();
        fightersPoolManagerThread.shutdown();
    }

    public BattleServerCredentialsForm addNewPlayerToBattleServer(PcPlayerForm player, int secretKey) {
        BattleServerConnectionHolder holder = connectionsHolderThread.getBattleServerConnectionHolder();
        return holder != null && holder.addNewPlayerToBattleServer(player, secretKey) ? holder.getServerCredentials() : null;
    }

    public boolean createBattle(BattleLifeCycleListener listener, BattleOrder2 battleOrder) {
        BattleServerConnectionHolder holder = connectionsHolderThread.getBattleServerConnectionHolder();
        return holder != null && holder.createBattle(listener, battleOrder);
    }

/*
    public boolean createTutorialBattleWithNpc(BattleLifeCycleListener listener, BattleOrder2 battleOrder, PcPlayerForm pcPlayer, List<NpcPlayerForm> npcPlayers) {
        BattleServerConnectionHolder holder = connectionsHolderThread.getBattleServerConnectionHolder();
        return holder != null && holder.createTutorialBattleWithNpc(listener, battleOrder, pcPlayer, npcPlayers);
    }

    public boolean joinExistingBattle(BattleOrder2 battleOrder, PcPlayerForm pcPlayer) {
        BattleServerConnectionHolder holder = connectionsHolderThread.getBattleServerConnectionHolder();
        return holder != null && holder.joinExistingBattle(battleOrder, pcPlayer);
    }

    public boolean viewExistingBattle(BattleOrder2 battleOrder, PcPlayerForm pcPlayer) {
        BattleServerConnectionHolder holder = connectionsHolderThread.getBattleServerConnectionHolder();
        return holder != null && holder.viewExistingBattle(battleOrder, pcPlayer);
    }
*/

/*
    public boolean sendItems(FastMap<Integer, Item> allItems) {
        BattleServerConnectionHolder holder = connectionsHolderThread.getBattleServerConnectionHolder();
        return holder != null && holder.sendItems(allItems);
    }
*/

    public boolean addNpcPlayersToBattleServer(List<NpcPlayerForm> npcPlayers) {
        BattleServerConnectionHolder holder = connectionsHolderThread.getBattleServerConnectionHolder();
        return holder != null && holder.addNpcPlayersToBattleServer(npcPlayers);
    }
}
